﻿using GalaSoft.MvvmLight;
using Cleverscape.UTorrentClient.WebClient;
using GalaSoft.MvvmLight.Threading;
using GalaSoft.MvvmLight.Command;
using System.Windows.Controls;
using uTorrentSL.Views;
using System.Windows;
using System;
using System.Windows.Markup;
using GalaSoft.MvvmLight.Messaging;

namespace uTorrentSL.ViewModel
{
    /// <summary>
    /// This class contains properties that the main View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class MainViewModel : MyBaseViewModel
    {
        public string Welcome
        {
            get
            {
                return "Welcome to MVVM Light";
            }
        }

        public RelayCommand<string> SetControl
        {
            get;
            private set;
        }


        /// <summary>
        /// The <see cref="MainContent" /> property's name.
        /// </summary>
        public const string MainContentPropertyName = "MainContent";

        private Uri _mainContent = new System.Uri("/Views/Home.xaml", System.UriKind.Relative);

        /// <summary>
        /// Gets the MainContent property.
        /// TODO Update documentation:
        /// Changes to that property's value raise the PropertyChanged event. 
        /// This property's value is broadcasted by the Messenger's default instance when it changes.
        /// </summary>
        public Uri MainContent
        {
            get
            {
                return _mainContent;
            }

            set
            {
                if (_mainContent == value)
                {
                    return;
                }

                var oldValue = _mainContent;
                _mainContent = value;

                // Remove one of the two calls below
                //throw new NotImplementedException();

                // Update bindings, no broadcast
                RaisePropertyChanged(MainContentPropertyName);

                // Update bindings and broadcast change using GalaSoft.MvvmLight.Messenging
                //RaisePropertyChanged(MainContentPropertyName, oldValue, value, true);
            }
        }

        /// <summary>
        /// Initializes a new instance of the MainViewModel class.
        /// </summary>
        public MainViewModel()
        {
            //data.InitialLoadComplete += new System.EventHandler(MainViewModel_InitialLoadComplete);
            //this.Torrents = data.Torrents;
            
            if (IsInDesignMode)
            {
                // Code runs in Blend --> create design time data.
                
            }
            else
            {
                // Code runs "for real"
                
            }

            SetControl = new RelayCommand<string>(LoadView);
            //((s) =>
            //{
            //    this.MainContent = (UIElement)Activator.CreateInstance(Type.GetType("UTorrentMVVMSL4.Views."+s) );
            //});

            Messenger.Default.Register<string>(this, "LoadView", LoadView);
        }

        private void LoadView(string viewName)
        {
            DispatcherHelper.UIDispatcher.BeginInvoke(() =>
                {
                    //this.MainContent = new System.Uri("/Views/Settings.xaml", System.UriKind.Relative);
                    //this.MainContent = new System.Uri("/Views/"+viewName+".xaml", System.UriKind.Relative); //(UIElement)Activator.CreateInstance(Type.GetType("uTorrentSL.Views." + viewName));
                    ViewModelLocator.Navigate(viewName);
                });
        }
        //void MainViewModel_InitialLoadComplete(object sender, System.EventArgs e)
        //{
        //    DispatcherHelper.UIDispatcher.BeginInvoke(() =>
        //        {
        //            this.Torrents = ((UTorrentWebClient)sender).Torrents;
        //            this.Torrents.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Torrents_CollectionChanged);
        //        });
        //}

        //void Torrents_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        //{
        //    string stop = "here";
        //}

        ////public override void Cleanup()
        ////{
        ////    // Clean up if needed

        ////    base.Cleanup();
        ////}
    }
}